using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Text))]
public class NumericalEffect : MonoBehaviour
{
	private Text m_Text;

	private string m_TextValue;

	private float m_Value;

	public int value = -1;

	public bool playAudio;

	[SerializeField]
	private string m_Pattern;

	[SerializeField]
	private float m_RollSpeed;

	private void Awake()
	{
		m_Text = GetComponent<Text>();
		Pattern();
	}

	public void InitValue(int val)
	{
		m_Value = val;
		value = val;
		m_TextValue = value.ToString();
		m_Text.text = m_TextValue;
	}

	private void Update()
	{
		if (m_Value == (float)value)
		{
			return;
		}
		if (m_Value < (float)value)
		{
			m_Value += Time.deltaTime * m_RollSpeed;
			if (playAudio)
			{
				AudioManager.PlayLoopClip("Scoreup");
			}
			if (m_Value > (float)value)
			{
				m_Value = value;
				if (playAudio)
				{
					AudioManager.StopLoopClip("Scoreup");
				}
			}
		}
		else if (m_Value > (float)value)
		{
			m_Value -= Time.deltaTime * m_RollSpeed;
			if (playAudio)
			{
				AudioManager.PlayLoopClip("Scoreup");
			}
			if (m_Value < (float)value)
			{
				m_Value = value;
				if (playAudio)
				{
					AudioManager.StopLoopClip("Scoreup");
				}
			}
		}
		Pattern();
	}

	private void LateUpdate()
	{
		if (m_Text.text.CompareTo(m_TextValue) != 0)
		{
			m_Text.text = m_TextValue;
		}
	}

	private void Pattern()
	{
		if (string.IsNullOrEmpty(m_Pattern))
		{
			m_TextValue = ((int)m_Value).ToString();
		}
		else
		{
			m_TextValue = string.Format(m_Pattern, (int)m_Value);
		}
		m_Text.text = m_TextValue;
	}

	public void OnApplicationPause(bool pause)
	{
		if (pause && playAudio)
		{
			AudioManager.StopLoopClip("Scoreup");
		}
	}
}
